import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/api/search/search_api.dart';
import 'package:flutter_book/constant/color.dart';
import 'package:flutter_book/constant/style.dart';
import 'package:flutter_book/widget/common/app_icon_button.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';
import 'package:flutter_book/util/extensions/widget_extensions.dart';

class SearchHistoryTextPage extends StatelessWidget {
  final List<String> searchHistoryList = [];
  final List<String> searchHotList = [];
  final Function(String text) clickTextListener;

  SearchHistoryTextPage(this.clickTextListener);

  @override
  Widget build(BuildContext context) {
    initData();
    return Container(
      padding: EdgeInsets.only(left: 16, right: 16, top: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _getSearchItemUi("搜索历史", searchHistoryList, isShowDeleteIcon: true, onDeleteListener: () {
            "清除历史".print;
          }),
          SizedBox(height: 28),
          // FutureBuilder(
          //     initialData:searchHotList,
          //     future: SearchApi.init().getHotSearchWord(),
          //     builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
          //       if (snapshot.connectionState == ConnectionState.done) {
          //         if (snapshot.hasError) {
          //           snapshot.error.toString().print;
          //           snapshot.error.toString().dialog;
          //           return  Container();
          //         }
          //         if (snapshot.data==null||snapshot.data!.length<1) {
          //           return Container();
          //         }
          //         return  _getSearchItemUi("热门搜索", snapshot.data as List<String>);
          //       }
          //       return Container();
          //     }
          // ),
        ],
      ),
    );
  }

  //搜索item
  Widget _getSearchItemUi(String title, List<String> dataList,
      {bool isShowDeleteIcon = false, Function()? onDeleteListener}) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(title, style: AppStyle.textMedium23),
                Visibility(
                  visible: isShowDeleteIcon,
                  child: AppIconButton(
                    iconSize: 20,
                    icon: Icons.history_outlined,
                    iconColor: AppColor.textHint,
                    buttonHeight: 32,
                    buttonWidth: 32,
                    onTap: onDeleteListener,
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 18),
          Wrap(
            alignment: WrapAlignment.start,
            children: List.generate(dataList.length, (index) => _filterItem(dataList[index])).toList(),
          ),
        ],
      ),
    );
  }

  //每一个按钮
  Widget _filterItem(String content) {
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 6),
      color: AppColor.transparent,
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(14)),
        child: Material(
          borderRadius: BorderRadius.all(Radius.circular(14)),
          color: AppColor.black.withOpacity(0.08),
          child: new InkWell(
            child: Container(
              child: Text(
                content,
                style: TextStyle(fontSize: 14, color: AppColor.textPrimary2),
              ),
              padding: const EdgeInsets.only(left: 24, top: 6, right: 24, bottom: 6),
            ),
            splashColor: AppColor.textHint,
            onTap: (){
              clickTextListener(content);
            },
          ),
        ),
      ),
    );
  }

  initData() {
    if (searchHistoryList.length < 1) {
      searchHistoryList.add("俗世妖人");
      searchHistoryList.add("hh1");
      searchHistoryList.add("都抖落");
      searchHistoryList.add("堕落大量");
      searchHistoryList.add("堕");
      searchHistoryList.add("斗破苍穹");
      searchHistoryList.add("看ASJFHAB");
      searchHistoryList.add("看");
    }
  }
}
